define(['zepto', 'underscore', 'JST', 'ctx', 'utils', 'wxConfig', 'wxShare', 'iscroll', 'lazyLoad', 'suiExtend', 'sui'], 
function($, _, JST, ctx, utils, wxConfig, wxShare, IScroll, lazyLoad) {
		return {
				init: function() {
					this.model = { 'page': 1 };
					this.initSwiper();
					this.onMenuClick();
					this.compileArticlePage();
					this.productScroll();
					lazyLoad.piclazyload();

					wxConfig.config(
						'onMenuShareTimeline',
						'onMenuShareAppMessage',
						'onMenuShareQQ',
						'onMenuShareWeibo',
						'onMenuShareQZone'
					);
					wxShare.openShare("人马君首页", "人马君：重塑身材，重塑生活");
				},

				initSwiper: function() {
				  	$(".swiper-container").swiper();
				},

				onMenuClick: function(){
					$(document).on("click", ".header-menu", function() {
					  $.openPanel("#panel-right-menu");
					});
				},

				extractQueryParameters: function(){
						var params = {}
						window.location.search
							.slice(1)
							.split('&')
							.forEach(function(query) {
								var param = query.split('=')
								params[param[0]] = param[1]
							})
						return params
			  },

				getshoplist: function() {
						var that = this;
						var defer = $.Deferred(),
							url = ctx.host + 'products'

						$.showIndicator()

						// request for rounds data
						$.ajax({
								url: url,
								type: 'GET',
								cache: false
						})
						.done(function(response) {
								$.hideIndicator();		//hide request indicator
								utils.processResult(response, function(result){
										defer.resolve(result.data)
								}, function(result){
										$.toast(result.message || '获取标题栏目模块失败')
								})
						})

			      return defer.promise()
			  },

				getArticle: function() {
						var that = this;
						var defer = $.Deferred(),
							url   = ctx.host + 'articles?page=1&m=1&per_page=4'

						// show request indicator
						$.showIndicator();
						// request  for rounds data
						$.ajax({
							url: 	url,
							type:  'GET',
							cache:  false
						})
						.done(function(response) {
								$.hideIndicator();
								utils.processResult(response, function(result){
										defer.resolve(result.data)
								}, function(result){
										$.toast(result.message || '获取文章模块失败')
								})
						})
					
						return defer.promise()
				},

				compileArticlePage: function(page) {
						var defershop = this.getshoplist(),
							deferArticle = this.getArticle();
						$.when(defershop).done(function(shopData) {
								// add data to menu model & page template
								var products = shopData.products.filter(function(product) {
									var tags = product.tags
									return !_.isEmpty(tags) && tags.hot
								})
								var $shoplist = JST.shopTpl({
										'products': products,
										'domain': ctx.domain,
										'imgsurl': ctx.cdn
								})

								$(".shop-wrap").empty().append($shoplist)
						})

						$.when(deferArticle).done(function(articleData){
								//add data to article modal & page template
								$article =  JST.indexTpl({
										'articles'  : articleData.articles,
										'domain'	: ctx.domain,
										'imgsurl'	: ctx.cdn
								})
								
								$("#fitstrat-goods").empty().append($article)
						})
				},

				productScroll: function() {
						new IScroll("#sku", {
								scrollX: true,
								scrollY: false,
								eventPassthrough: true
						}).on('scrollEnd', function(e) {
								if (this.maxScrollX < 0 && (this.startX + this.distX) <= (this.maxScrollX - 150)) {
										var more = this.scroller.querySelector('.shop-more > a')
										if (more) {
												window.location.href = $(more).attr("href")
										}
								}
						})
				}
		}
})


